<template>
  <el-dialog :title="form.id ? $t('common.editBtn') :  $t('common.addBtn')" v-model="visible" width="800"
             :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef  label-width="105px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('qms.testItem')" prop="checkProjectName">
            <el-input v-model="form.checkProjectName" :placeholder="$t('qms.testItem')"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('qms.testItemCode')" prop="checkProjectCode">
            <el-input v-model="form.checkProjectCode" :placeholder="$t('qms.testItemCode')"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('qms.CheckType')" prop="checkTypeName">
            <el-select :placeholder="$t('qms.CheckType')"
                       filterable
                       remote
                       @change="changeCheckType"
                       :remote-method="getCheckType"
                       v-model="form.checkTypeName">
              <el-option v-for="item in checkTypeList"
                         :key="item.text"
                         :label="item.text"
                         :value="item">
                <span style="float: left">{{ item.text }}</span>
                <span style=" float: right;  color: var(--el-text-color-secondary);  font-size: 13px; "> {{
                    item.value
                  }} </span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('qms.checkTypeCode')" prop="checkTypeCode">
            <el-input disabled v-model="form.checkTypeCode" :placeholder="$t('qms.checkTypeCode')"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('qms.testMethod')" prop="checkFunction">
            <el-input v-model="form.checkFunction" :placeholder="$t('qms.testMethod')"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('qms.testCondition')" prop="checkCondition">
            <el-input v-model="form.checkCondition" :placeholder="$t('qms.testCondition')"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('qms.resultType')" prop="resultType">
            <el-select :placeholder="$t('qms.resultType')"
                       filterable
                       remote
                       @change="changeResultType"
                       :remote-method="getResultType"
                       v-model="form.resultType">
              <el-option v-for="item in resultTypeList"
                         :key="item.value"
                         :label="item.value"
                         :value="item">
                <span style="float: left">{{ item.value }}</span>
                <span style=" float: right;  color: var(--el-text-color-secondary);  font-size: 13px; "> {{
                    item.text
                  }} </span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('qms.standardValue')" prop="standardItem">
            <el-input v-model="form.standardItem" :placeholder="$t('qms.standardValue')"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('iot.unit')" prop="unit">
            <el-input v-model="form.unit" :placeholder="$t('iot.unit')"/>
          </el-form-item>
        </el-col>


      </el-row>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">{{$t('operation.Cancel')}}</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">{{$t('operation.confirm')}}</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import {useMessage} from "/@/hooks/message";
import {addObj, getObj, putObj} from '/@/api/platform/qms/qmsCheckProjectDetails'
import {fetchCheckList} from "/@/api/platform/qms/qmsRelationH";
import {useI18n} from "vue-i18n";

const {t} = useI18n();

const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)

const checkTypeList = ref()
const resultTypeList = ref()
// 定义字典

// 提交表单数据
const form = reactive({
  id: '',
  checkProjectName: '',
  checkProjectCode: '',
  checkTypeName: '',
  checkTypeCode: '',
  resultType: '',
  standardItem: '',
  unit: '',
  checkFunction: '',
  checkCondition: '',
  creationTime: '',
  createUserNo: '',
  createUserName: '',
  lastModifyTime: '',
  lastModifyUserName: '',
  lastModifyUserNo: '',
  attribute1: '',
  attribute2: '',
  attribute3: '',
});

const getResultType = () => {
  fetchCheckList({
    "code": "QmsResultType"
  }).then((res: any) => {
    if (res.code == 0) {
      resultTypeList.value = res.data
    }
  })
}

const getCheckType = () => {
  fetchCheckList({
    "code": "QMSInspectionCategory"
  }).then((res: any) => {
    if (res.code == 0) {
      checkTypeList.value = res.data
    }
  })
}

const changeResultType = (data) => {
  form.resultType = data.value
  if (data.value === 'option') {
    form.unit = 'NA'
    dataRules.value = {
      checkProjectName: [
        {required: true, trigger: 'blur'}
      ],
      checkProjectCode: [
        {required: true, trigger: 'blur'}
      ], checkTypeName: [
        {required: true, trigger: 'blur'}
      ], unit: [
        {required: true, trigger: 'blur'}
      ], resultType: [
        {required: true, trigger: 'blur'}
      ]
    }
  } else {
    dataRules.value = {
      checkProjectName: [
        {required: true, trigger: 'blur'}
      ],
      checkProjectCode: [
        {required: true, trigger: 'blur'}
      ], checkTypeName: [
        {required: true, trigger: 'change'}
      ], unit: [
        {required: true, trigger: 'blur'}
      ], resultType: [
        {required: true, trigger: 'blur'}
      ], standardItem: [
        {required: true, trigger: 'blur'}
      ],
    }
  }
}

// 定义校验规则
const dataRules = ref({
  checkProjectName: [
    {required: true, trigger: 'blur'}
  ],
  checkProjectCode: [
    {required: true, trigger: 'blur'}
  ], checkTypeName: [
    {required: true, trigger: 'blur'}
  ], unit: [
    {required: true, trigger: 'blur'}
  ], resultType: [
    {required: true, trigger: 'blur'}
  ], standardItem: [
    {required: true, trigger: 'blur'}
  ],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取qmsCheckProjectDetails信息
  if (id) {
    form.id = id
    getqmsCheckProjectDetailsData(id)
  }
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;
  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? t('qms.modifiedSuccessfully') : t('qms.addSuccessfully'));
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};

// 改变检验类型方法
const changeCheckType = (data) => {
  form.checkTypeName = data.text
  form.checkTypeCode = data.value
}

// 初始化表单数据
const getqmsCheckProjectDetailsData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>